package arithmetic.socket.netty.websocket;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
import io.netty.handler.stream.ChunkedWriteHandler;

/**
 * @author jiangfeng
 * Created on 2020-03-04
 */
public class WebSocketServerInitialer extends ChannelInitializer<SocketChannel> {

    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        ChannelPipeline pipeline=  ch.pipeline();
        pipeline. addLast(new HttpServerCodec());
        // 块状写
        pipeline. addLast(new ChunkedWriteHandler());
        // 聚合处理
        pipeline .addLast(new HttpObjectAggregator(8192));
        pipeline. addLast(new WebSocketServerProtocolHandler("/ws" ));
        pipeline .addLast(new WebSocketHandler());
    }
}
